package com.thinkingstar.iads.projectcenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.validation.Path.ReturnValueNode;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.datacenter.entity.DcProjectMentionInformation;
import com.thinkingstar.iads.projectcenter.entity.PcProjectInfo;


/**
 * @ClassName: DaoPcProjectData 
 * @Description:项目中心_项目_项目基础信息维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoPcProjectData extends EntityRepositoryServiceImpl<PcProjectInfo>{

	public DaoPcProjectData(Dao dao) {
		super(dao);
	}
	
	public QueryResult findAllProjectInfoList(NutMap nutMap){
		List <Map> page_list = null;
		Pager pager = null;
		StringBuffer stringBuffer = new StringBuffer("select "
				+ " ppi.* , case ppi.del_flag when '0' then '生效' else '失效' end project_state ,"
				+ " dpdp.project_design_phase_name ,  dpdm.project_design_major_name  "
				+ " from pc_project_info ppi  "
				+ " left join dc_project_design_phase dpdp on ppi.project_design_phase_id = dpdp.id and dpdp.del_flag = '0'"
				+ " left join dc_project_design_major dpdm on ppi.project_design_major_id = dpdm.id and dpdm.del_flag = '0'"
				+ " where ppi.del_flag = '0' ");
		if(nutMap.get("project_name") != null){
			stringBuffer.append(" and ppi.project_name like @project_name ");
		}
		stringBuffer.append(" order by ppi.create_time desc ");
		Sql sql = Sqls.create(stringBuffer.toString());
		if(nutMap.get("project_name") != null){
			sql.params().set("project_name", "%"+nutMap.getString("project_name")+"%");
		}
		if(nutMap.get("page_num") != null && nutMap.get("page_size") != null){
			pager = new Pager(nutMap.getInt("page_num"), nutMap.getInt("page_size"));
			pager.setRecordCount(findAllProjectNum(nutMap));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			List<Map <String , Object>> list = new LinkedList<>();
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next()){
					Map <String , Object> map = new HashMap<>();
					map.put("id", rs.getString("id"));
					map.put("project_code", rs.getString("project_code"));
					map.put("project_name", rs.getString("project_name"));
					map.put("project_short_name", rs.getString("project_short_name"));
					map.put("project_user_name", rs.getString("project_user_name"));
					map.put("project_state", rs.getString("project_state"));
					map.put("project_design_phase_id", rs.getString("project_design_phase_id"));
					map.put("project_design_content_id", rs.getString("project_design_content_id"));
					map.put("project_design_major_id", rs.getString("project_design_major_id"));
					map.put("project_design_phase_name", rs.getString("project_design_phase_name"));
					map.put("project_design_content_name", rs.getString("project_design_content_name"));
					map.put("project_design_major_name", rs.getString("project_design_major_name"));
					map.put("create_time", rs.getString("create_time"));
					map.put("update_time", rs.getString("update_time"));
					list.add(map);
				}
				return list;
			}
		});
		dao().execute(sql);
		page_list = sql.getList(Map.class);
		return new QueryResult(page_list,pager);
	}
	
	public int findAllProjectNum(NutMap nutMap){
		StringBuffer stringBuffer = new StringBuffer("select count(ppi.id) num "
				+ " from pc_project_info ppi  "
				+ " left join dc_project_design_phase dpdp on ppi.project_design_phase_id = dpdp.id and dpdp.del_flag = '0'"
				+ " left join dc_project_design_major dpdm on ppi.project_design_major_id = dpdm.id and dpdm.del_flag = '0'"
				+ " where ppi.del_flag = '0' ");
		if(nutMap.get("project_name") != null){
			stringBuffer.append(" and ppi.project_name like @project_name ");
		}
		Sql sql = Sqls.create(stringBuffer.toString());
		if(nutMap.get("project_name") != null){
			sql.params().set("project_name", "%"+nutMap.getString("project_name")+"%");
		}
		sql.setCallback(new SqlCallback() {
			int count = 0;
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next())
				count = rs.getInt("num");
				return count;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}

}
